﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Edit.aspx.cs" Inherits="CanDoo.Oil.WebApp.buyInOrder.Edit" %>

<%@ Register Src="../UserControls/SelectSupplier.ascx" TagPrefix="uc1" TagName="SelectSupplier" %>
<%@ Register Src="../UserControls/SelectWarehouse.ascx" TagPrefix="uc1" TagName="SelectWarehouse" %>
<%@ Register Src="../UserControls/SelectUser.ascx" TagPrefix="uc1" TagName="SelectUser" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>采购单编辑</title>
    <link href="/res/css/common.css" rel="stylesheet" />
        <style>
        .f-grid-row-summary .f-grid-cell-inner {
            font-weight: bold;
            color: red;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <f:PageManager ID="PageManager1" AutoSizePanelID="PanelMain" runat="server" />

        <f:Panel ID="PanelMain" ShowBorder="false" ShowHeader="false" AutoScroll="true" runat="server" Layout="VBox" BoxConfigAlign="Stretch" BoxConfigPosition="Start">
            <Toolbars>
                <f:Toolbar ID="Toolbar1" runat="server">
                    <Items>
                        <f:Button ID="btnClose" EnablePostBack="false" runat="server" Hidden="true"
                            Text="关闭">
                        </f:Button>
                        <f:Button ID="btnSave" ValidateForms="FormEdit" Hidden="true"
                            OnClick="btnSave_Click" runat="server" Text="保存">
                        </f:Button>
                        <f:Button ID="btnToEdit" Hidden="true"
                            OnClick="btnToEdit_Click" runat="server" Text="编辑信息">
                        </f:Button>
                        <f:HiddenField ID="ed_EntityId" runat="server"></f:HiddenField>

                    </Items>
                </f:Toolbar>
            </Toolbars>
            <Items>
                <f:Form BodyPadding="10px" ShowBorder="false" EnableCollapse="true"
                    ID="FormEdit" runat="server" ShowHeader="false" LabelAlign="Right">
                    <Items>
                       <f:Panel ID="Panel1" ShowHeader="false" ShowBorder="false" runat="server" Layout="Column" BoxConfigAlign="Stretch" Height="50px">
                            <Items>
                                <f:Image ID="ed_barcode" runat="server" ImageWidth="300px" Label="单号条码"></f:Image>
                            </Items>
                        </f:Panel>
                        <f:Panel ID="Panel0" ShowHeader="false" ShowBorder="false" runat="server" Layout="Column" BoxConfigAlign="Stretch">
                            <Items>
                                <f:TextBox ID="ed_Code" runat="server" Label="采购单号" MaxLength="50" ColumnWidth="25%" Readonly="true" EmptyText="系统自动生成">
                                </f:TextBox>
                                <f:DatePicker ID="ed_OrderDate" runat="server" Required="true" ShowRedStar="True" Label="采购日期" ColumnWidth="25%">
                                </f:DatePicker>
                                <f:DatePicker ID="ed_RequestDate" runat="server" Required="true" ShowRedStar="True" Label="要求到货" ColumnWidth="25%">
                                </f:DatePicker>
                            </Items>
                        </f:Panel>
                        <f:Panel ID="Panel4" ShowHeader="false" ShowBorder="false" runat="server" Layout="Column" BoxConfigAlign="Stretch">
                            <Items>
                                <f:DropDownBox runat="server" ID="ed_Supplier" ColumnWidth="25%" EmptyText="请选择供应商" DataControlID="SelectSupplierControl_GridList" EnableMultiSelect="false" MatchFieldWidth="false" ShowLabel="true" AutoShowClearIcon="true" Required="true" ShowRedStar="True" Label="供应商" OnTextChanged="ed_Supplier_TextChanged" AutoPostBack="true" OnClearIconClick="ed_Supplier_TextChanged" EnableClearIconClickEvent="true">
                                    <PopPanel>
                                        <f:UserControlConnector runat="server">
                                            <uc1:SelectSupplier runat="server" ID="SelectSupplier" />
                                        </f:UserControlConnector>
                                    </PopPanel>
                                </f:DropDownBox>
                                <f:DropDownBox runat="server" ID="ed_Warehouse" ColumnWidth="25%" EmptyText="请选择仓库" DataControlID="SelectWarehouseControl_GridList" EnableMultiSelect="false" MatchFieldWidth="false" ShowLabel="true" AutoShowClearIcon="true" Required="true" ShowRedStar="True" Label="仓库">
                                    <PopPanel>
                                        <f:UserControlConnector runat="server">
                                            <uc1:SelectWarehouse runat="server" ID="SelectWarehouse" />
                                        </f:UserControlConnector>
                                    </PopPanel>
                                </f:DropDownBox>
                                <f:DropDownBox runat="server" ID="ed_User" ColumnWidth="25%" EmptyText="请选择采购员" DataControlID="SelectUserControl_GridList" EnableMultiSelect="false" MatchFieldWidth="false" ShowLabel="true" AutoShowClearIcon="true" Required="true" ShowRedStar="True" Label="采购员">
                                    <PopPanel>
                                        <f:UserControlConnector runat="server">
                                            <uc1:SelectUser runat="server" ID="SelectUser" />
                                        </f:UserControlConnector>
                                    </PopPanel>
                                </f:DropDownBox>
                            </Items>
                        </f:Panel>
                        <f:Panel ID="Panel8" ShowHeader="false" ShowBorder="false" runat="server" Layout="Column" BoxConfigAlign="Stretch">
                            <Items>
                                <f:NumberBox ID="ed_Num" Readonly="true" runat="server" Required="true" ShowRedStar="True" Label="采购数量" ColumnWidth="25%">
                                </f:NumberBox>
                                <f:NumberBox ID="ed_Money" Readonly="true" runat="server" Required="true" ShowRedStar="True" Label="采购金额" ColumnWidth="25%">
                                </f:NumberBox>
                                <f:NumberBox ID="ed_PaymentDays" runat="server" Required="true" ShowRedStar="True" Label="账期" ColumnWidth="25%" OnTextChanged="ed_PaymentDays_TextChanged" AutoPostBack="true">
                                </f:NumberBox>
                                <f:DatePicker ID="ed_AppointPayDate" runat="server" Required="true" ShowRedStar="True" Label="约定付款" ColumnWidth="25%">
                                </f:DatePicker>
                            </Items>
                        </f:Panel>
                        <f:Panel ID="Panel14" ShowHeader="false" ShowBorder="false" runat="server" Layout="Column" BoxConfigAlign="Stretch">
                            <Items>
                                <f:TextArea ID="ed_Remark" runat="server" Label="备注" Height="60px" MaxLength="500">
                                </f:TextArea>
                            </Items>
                        </f:Panel>
                    </Items>
                </f:Form>
                <f:Grid ID="GridChild" ShowBorder="true" ShowHeader="true" Title="采购单明细" EnableCollapse="true" MinHeight="400px" BodyPadding="5px"
                    runat="server" DataKeyNames="Id" AllowCellEditing="true" EnableColumnLines="true" ShowSelectedCell="true" ClicksToEdit="1" BoxFlex="1" TabEditableCell="true" EnableSummary="true" SummaryPosition="Flow">
                    <Toolbars>
                        <f:Toolbar ID="Toolbar2" runat="server">
                            <Items>
                                <f:Button ID="btnNew" Text="新增数据" Icon="Add" EnablePostBack="false" runat="server">
                                    <Listeners>
                                        <f:Listener Event="click" Handler="onAddClick" />
                                    </Listeners>
                                </f:Button>
                                <f:Button ID="btnDelete" Text="删除选中行" Icon="Delete" EnablePostBack="false" runat="server">
                                    <Listeners>
                                        <f:Listener Event="click" Handler="onDeleteClick" />
                                    </Listeners>
                                </f:Button>
                            </Items>
                        </f:Toolbar>
                    </Toolbars>
                    <Columns>
                        <f:TemplateField Width="40px">
                            <ItemTemplate>
                                <asp:Label ID="Label5" runat="server" Text='<%# Container.DataItemIndex + 1 %>'></asp:Label>
                            </ItemTemplate>
                        </f:TemplateField>
                        <f:RenderField Width="50px" ColumnID="ProductId" DataField="ProductId" FieldType="String" HeaderText="商品ID" Hidden="true">
                        </f:RenderField>
                        <f:RenderField Width="100px" ColumnID="Code" DataField="Code" HeaderText="商品编码">
                            <Editor>
                                <f:TriggerBox ID="ed_child_Code" TriggerIcon="Search" EnableEdit="false" OnTriggerClick="ed_Name_TriggerClick" runat="server">
                                </f:TriggerBox>
                            </Editor>
                        </f:RenderField>
                        <f:RenderField Width="150px" ColumnID="Name" DataField="Name" HeaderText="商品">
                            <Editor>
                                <f:TriggerBox ID="ed_child_Name" TriggerIcon="Search" EnableEdit="false" OnTriggerClick="ed_Name_TriggerClick" runat="server">
                                </f:TriggerBox>
                            </Editor>
                        </f:RenderField>
                        <f:RenderField Width="150px" ColumnID="SN" DataField="SN" FieldType="String" HeaderText="条码">
                        </f:RenderField>
                        <f:RenderField Width="150px" ColumnID="Model" DataField="Model" FieldType="String" HeaderText="型号">
                        </f:RenderField>
                        <f:RenderField Width="150px" ColumnID="Specifications" DataField="Specifications" FieldType="String" HeaderText="规格">
                        </f:RenderField>
                        <f:RenderField Width="50px" ColumnID="Unit" DataField="Unit" FieldType="String" HeaderText="单位">
                        </f:RenderField>
                        <f:RenderField Width="80px" ColumnID="Price" DataField="Price" FieldType="double"
                            HeaderText="单价">
                            <Editor>
                                <f:NumberBox ID="ed_child_Price" runat="server" Required="true" MinValue="0">
                                </f:NumberBox>
                            </Editor>
                        </f:RenderField>
                        <f:RenderField Width="80px" ColumnID="Num" DataField="Num" FieldType="double"
                            HeaderText="数量">
                            <Editor>
                                <f:NumberBox ID="ed_child_Num" runat="server" Required="true" MinValue="0">
                                </f:NumberBox>
                            </Editor>
                        </f:RenderField>
                        <f:RenderField Width="120px" ColumnID="Money" DataField="Money" FieldType="double"
                            HeaderText="金额">
                        </f:RenderField>
                        <f:RenderField Width="80px" ColumnID="ChangeNum" DataField="ChangeNum" FieldType="double"
                            HeaderText="入库数量">
                        </f:RenderField>
                        <f:RenderField Width="80px" ColumnID="ChangeMoney" DataField="ChangeMoney" FieldType="double"
                            HeaderText="入库金额">
                        </f:RenderField>
                        <f:RenderField Width="150px" ColumnID="Remark" DataField="Remark" FieldType="String"
                            HeaderText="备注">
                            <Editor>
                                <f:TextBox ID="ed_child_Remark" runat="server">
                                </f:TextBox>
                            </Editor>
                        </f:RenderField>
                        <f:RenderField ColumnID="Delete" Width="80px" RendererFunction="renderDelete"></f:RenderField>
                    </Columns>
                    <Listeners>
                        <f:Listener Event="afteredit" Handler="onGridAfterEdit" />
                    </Listeners>
                </f:Grid>
            </Items>
        </f:Panel>
        <f:Window ID="WindowSearch" Hidden="true" EnableIFrame="true" EnableMaximize="true"
            EnableResize="true" Target="Top" runat="server" Height="500px" Width="900px"
            Title="选择商品" OnClose="WindowSearch_Close">
        </f:Window>
    </form>
    <script>
        var gridChildClientID = '<%= GridChild.ClientID %>';

        function renderDelete() {
            return '<a href="javascript:;" class="action-btn delete"><img class="f-grid-imagefield" src="/res/icon/delete.png"></a>';
        }

        function updateSummary() {
            var me = F(gridChildClientID), numTotal = 0, moneyTotal = 0;
            me.getRowEls().each(function (index, tr) {
                numTotal += me.getCellValue(tr, 'Num') * 1.0;
                moneyTotal += me.getCellValue(tr, 'Money') * 1.0;
            });

            // 第三个参数 true，强制更新，不显示左上角的更改标识
            me.updateSummaryCellValue('Num', numTotal, true);
            me.updateSummaryCellValue('Money', moneyTotal, true);

            F('<%= ed_Num.ClientID %>').setValue(numTotal);
            F('<%= ed_Money.ClientID %>').setValue(moneyTotal);
        }

        function onGridAfterEdit(event, value, params) {
            if (params.columnId == "Price" || params.columnId == "Num") {
                var me = F(gridChildClientID);
                var p = params.rowData.values.Price * 1.0;
                var n = params.rowData.values.Num * 1.0;
                var m = p*n;

                me.getRowEls().each(function (index, tr) {
                    if (me.getCellValue(tr, 'Price') == p && me.getCellValue(tr, 'Num') == n) {
                        me.updateCellValue(tr, "Money", m);
                    }
                });
                updateSummary();
            }
        }

        // 点击新增按钮
        function onAddClick() {
            F(gridChildClientID).addNewRecord({}, true);
        }

        // 点击删除按钮
        function onDeleteClick() {
            var gridChild = F(gridChildClientID);
            if (!gridChild.hasSelection()) {
                F.alert('请至少选择一项！');
                return false;
            }

            F.confirm({
                message: '删除选中行？',
                ok: function () {
                    gridChild.deleteSelectedRows();
                    // 删除行后，要记得更新合计行
                    updateSummary();
                }
            });
        }

        // 页面渲染完毕
        F.ready(function () {
            // 注册表格行内删除事件
            var gridChild = F(gridChildClientID);
            gridChild.el.on('click', '.action-btn.delete', function () {
                var targetEl = $(this);
                var rowEl = targetEl.parents('.f-grid-row');
                var rowData = gridChild.getRowData(rowEl);

                F.confirm({
                    message: '删除选中行？',
                    ok: function () {
                        gridChild.deleteRow(rowData.id);
                        // 删除行后，要记得更新合计行
                        updateSummary();
                    }
                });
            });
            //加载完成后显示合计行
            updateSummary();
        });
    </script>
</body>
</html>
